package cn.ac.radi.userver.umodel.model;

import cn.ac.radi.userver.umodel.util.ProcessUtil;
import org.slf4j.Logger;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.IOException;

public class BaseLineModel {

    private Environment env = null;
    private Logger logger = null;

    private String dataPath = "";
    private String viewPath = "";
    private static final String COMMAND_TEMPLATE = "python evaluate_onep.py --val_datapth {input} --view_path {outputPath}";

    public BaseLineModel(Environment env, Logger logger){
        this.env = env;
        this.logger = logger;
    }

    public boolean run(String input, String output){
        boolean ret = true;

        String pyEnvPath = env.getProperty("userver.env.python_conda_env");
        String pyCodeHome = env.getProperty("userver.model.K2.python_home");

        String inputPath = env.getProperty("userver.model.K2.baseline.input");
        String outputPath = env.getProperty("userver.model.K2.baseline.output");

        inputPath = inputPath +"/" + input;
        outputPath = outputPath +"/" + output;

        String pyCmd = buildCommand(inputPath, outputPath);

        String command = "";
        if(!pyEnvPath.isEmpty()){
            //String condaCmd = "cmd /C conda activate " + pyEnvPath;

            String condaCmd = "";
            if(isWindows()){
                condaCmd += "cmd /C ";
            }
            else {
                condaCmd += "source activate &";
            }
            condaCmd += "conda activate " + pyEnvPath;
            command = condaCmd + " & " + pyCmd;
        }
        else {
            command = pyCmd;
        }

        return this.runCommandProcess(command, pyCodeHome, this.logger);
    }

    private String buildCommand(String inputPath, String outputPath){
        return COMMAND_TEMPLATE.replace("{input}", inputPath)
                .replace("{outputPath}", outputPath);
    }

    private boolean isWindows(){
        String os = System.getProperty("os.name");
        return os.toLowerCase().startsWith("win");
    }

    public static boolean runCommandProcess(String cmd, String homePath, Logger logger){

        logger.info("\t[python命令]：" + cmd);

        long ts = System.currentTimeMillis();   //获取开始时间

        logger.debug("~~~~~~~~~开始~~~~~~~~");

        boolean ret = false;
        Process proc = null;
        try{
            Runtime rt = Runtime.getRuntime();
            proc = rt.exec(cmd, null, new File(homePath));

            ProcessUtil pu = new ProcessUtil();
            pu.writeProcessStream(proc);

            ret = true;

            proc.waitFor();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        catch (RuntimeException e){
            e.printStackTrace();
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }
        finally {

            logger.debug("~~~~~~~~~结束~~~~~~~~");

            long te = System.currentTimeMillis();   //获取结束时间
            logger.info("运行时间：" + Long.toString(te-ts) + " 毫秒");

            if(proc!=null){
                logger.info("~~~~~~~~销毁processor~~~~~~~~");
                proc.destroy();
            }

            return ret;
        }
    }

}
